package com.jianguo.web.controller.user;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jianguo.user.domain.PzBalance;
import com.jianguo.user.service.IPzBalanceService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jianguo.common.annotation.Log;
import com.jianguo.common.core.controller.BaseController;
import com.jianguo.common.core.domain.AjaxResult;
import com.jianguo.common.enums.BusinessType;
import com.jianguo.user.domain.PzUser;
import com.jianguo.user.service.IPzUserService;
import com.jianguo.common.utils.poi.ExcelUtil;
import com.jianguo.common.core.page.TableDataInfo;

/**
 * 用户端用户Controller
 *
 * @author xjj
 * @date 2024-03-09
 */
@RestController
@RequestMapping("/user/user")
public class PzUserController extends BaseController
{
    @Autowired
    private IPzUserService pzUserService;

    @Autowired
    private IPzBalanceService pzBalanceService;

    /**
     * 查询用户端用户列表
     */
    @PreAuthorize("@ss.hasPermi('user:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(PzUser pzUser)
    {
        startPage();
        List<PzUser> list = pzUserService.selectPzUserList(pzUser);
        return getDataTable(list);
    }

    /**
     * 导出用户端用户列表
     */
    @PreAuthorize("@ss.hasPermi('user:user:export')")
    @Log(title = "用户端用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PzUser pzUser)
    {
        List<PzUser> list = pzUserService.selectPzUserList(pzUser);
        ExcelUtil<PzUser> util = new ExcelUtil<PzUser>(PzUser.class);
        util.exportExcel(response, list, "用户端用户数据");
    }

    /**
     * 获取用户端用户详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(pzUserService.getById(id));
    }

    /**
     * 新增用户端用户
     */
    @Log(title = "用户端用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PzUser pzUser) {
        if (ObjectUtil.isEmpty(pzUser.getPic())){
            pzUser.setPic("1");
        }
        boolean save = pzUserService.save(pzUser);
        if (save){
            PzBalance pzBalance = new PzBalance();
            pzBalance.setBalance(0L).setUserid(pzUser.getId());
            pzBalanceService.save(pzBalance);
        }
        return toAjax(save);
    }

    /**
     * 修改用户端用户
     */
    @PreAuthorize("@ss.hasPermi('user:user:edit')")
    @Log(title = "用户端用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PzUser pzUser) {
        return toAjax(pzUserService.updateById(pzUser));
    }

    /**
     * 删除用户端用户
     */
    @PreAuthorize("@ss.hasPermi('user:user:remove')")
    @Log(title = "用户端用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(pzUserService.removeByIds(Arrays.asList(ids)));
    }
}
